package com.xc.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xc.demo.common.Result;
import com.xc.demo.entity.Goods;
import com.xc.demo.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Slf4j
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    public GoodsService goodsService;


    //    保存或增加
    @PostMapping
    public Boolean saveCatgory(@RequestBody Goods goods){

        return   goodsService.saveOrUpdate(goods);

    }

//    批量删除接口
            @PostMapping("/delGoods/batch")
           public Boolean deleteBatch(@RequestBody List<Integer> ids){
    return goodsService.removeByIds(ids);
}


//    删除
    @DeleteMapping("/{id}")
    public Boolean deleteGoods(@PathVariable Integer id){
        return goodsService.removeById(id);
    }


    //实现分页效果
    @GetMapping("/page")
    public IPage<Goods> findPage(@RequestParam Integer pageNum,
                                 @RequestParam Integer pageSize,
                                 @RequestParam(defaultValue = "") String uname

    ){
        log.info("pageNum={},pageSize={},username={},accountName={}",pageNum,pageSize,uname);

        IPage<Goods> page= new Page<>(pageNum,pageSize);

        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();

        if(!"".equals(uname)){
            queryWrapper.like("name",uname);
        }


        queryWrapper.orderByAsc("id");

        return goodsService.page(page,queryWrapper);
    }





    @GetMapping("/update/onSale")
    public Result updateOnSale(@RequestParam Long id, @RequestParam Integer onSale){

        UpdateWrapper<Goods> updateWrapper = new UpdateWrapper<>();

        updateWrapper.lambda().set(Goods::getOnSale,onSale).in(Goods::getId,id);

        return Result.success(goodsService.update(updateWrapper));

    }



}
